package com.expert.miniapp.service.impl;

import com.expert.miniapp.dao.Laboratory;
import com.expert.miniapp.dao.LaboratoryMapper;
import com.expert.miniapp.service.LaboratoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 实验室业务逻辑实现类
 */
@Service
public class LaboratoryServiceImpl implements LaboratoryService {

    @Autowired
    private LaboratoryMapper laboratoryMapper;

    @Override
    public Laboratory getLaboratoryById(String labId) {
        if (labId == null || labId.trim().isEmpty()) {
            return null;
        }
        return laboratoryMapper.findById(labId);
    }

    @Override
    public List<Laboratory> getLaboratoriesByCategory(String category) {
        if (category == null || category.trim().isEmpty()) {
            return laboratoryMapper.findAll();
        }
        return laboratoryMapper.findByCategory(category);
    }

    @Override
    public PageInfo<Laboratory> getAllLaboratories(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<Laboratory> laboratories = laboratoryMapper.findAll();
        return new PageInfo<>(laboratories);
    }

    @Override
    public PageInfo<Laboratory> getRecommendedLaboratories(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<Laboratory> laboratories = laboratoryMapper.findByWeightOrder();
        return new PageInfo<>(laboratories);
    }

    @Override
    public PageInfo<Laboratory> searchLaboratories(String keyword, Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<Laboratory> laboratories = laboratoryMapper.searchByKeyword(keyword);
        return new PageInfo<>(laboratories);
    }
}